/***************************************************************************//**
 * @file GUI/ComboUnits.cpp
 * @author  Marek M. Cel <marekcel@mscsim.org>
 *
 * @section LICENSE
 *
 * Copyright (C) 2013 Marek M. Cel
 *
 * This file is part of MScSim. You can redistribute and modify it under
 * the terms of GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Further information about the GNU General Public License can also be found
 * on the world wide web at http://www.gnu.org.
 ******************************************************************************/
#ifndef COMBO_UNITS_CPP
#define COMBO_UNITS_CPP
#endif

////////////////////////////////////////////////////////////////////////////////

#include <Utils.h>

#include <GUI/ComboUnits.h>

////////////////////////////////////////////////////////////////////////////////

ComboUnits::ComboUnits( UnitsType unitsType, QWidget *parent ) :
    QComboBox ( parent ),
    m_convertFactor ( 1.0f ),
    m_unitsType ( Number )
{
    initialize();
    
    switch ( unitsType )
    {   
    //case Number: (sic!) handled by default
    
    case Length:
        m_unitsType = Length;
        for ( int i = 0; i < LEN_UNITS_COUNT; i++ ) addItem( m_lenNames[ i ] );
        break;

    case Velocity:
        m_unitsType = Velocity;
        for ( int i = 0; i < VEL_UNITS_COUNT; i++ ) addItem( m_velNames[ i ] );
        break;
        
    case Angle:
        m_unitsType = Angle;
        for ( int i = 0; i < ANG_UNITS_COUNT; i++ ) addItem( m_angNames[ i ] );
        break;
        
    case RotationRate:
        m_unitsType = RotationRate;
        for ( int i = 0; i < ANG_UNITS_COUNT; i++ ) addItem( m_rotNames[ i ] );
        break;
        
    default:
        m_unitsType = Number;
        for ( int i = 0; i < NUM_UNITS_COUNT; i++ ) addItem( m_numNames[ i ] );
        break;
    }
    
    connect( this, SIGNAL(currentIndexChanged(int)), this, SLOT(on_currentIndexChanged(int)) );
    
    setCurrentIndex( 0 );
}

////////////////////////////////////////////////////////////////////////////////
    
ComboUnits::~ComboUnits() {}

////////////////////////////////////////////////////////////////////////////////

void ComboUnits::initialize()
{
    // number
    m_numCoefs[ 0 ] = 1.0f;
    m_numNames[ 0 ] = QString( "-" );
        
    // length
    m_lenCoefs[ 0 ] = 1.0f;
    m_lenNames[ 0 ] = QString( "m" );
    
    m_lenCoefs[ 1 ] = (float)Utils::m2ft();
    m_lenNames[ 1 ] = QString( "ft" );
    
    m_lenCoefs[ 2 ] = (float)Utils::m2km();
    m_lenNames[ 2 ] = QString( "km" );
    
    m_lenCoefs[ 3 ] = (float)Utils::m2nm();
    m_lenNames[ 3 ] = QString( "nm" );
    
    // velocity
    m_velCoefs[ 0 ] = 1.0f;
    m_velNames[ 0 ] = QString( "m/s" );
    
    m_velCoefs[ 1 ] = (float)Utils::mps2fpm();
    m_velNames[ 1 ] = QString( "ft/min" );
    
    m_velCoefs[ 2 ] = (float)Utils::mps2fps();
    m_velNames[ 2 ] = QString( "ft/s" );
    
    m_velCoefs[ 3 ] = (float)Utils::mps2kmh();
    m_velNames[ 3 ] = QString( "km/h" );
    
    m_velCoefs[ 4 ] = (float)Utils::mps2kts();
    m_velNames[ 4 ] = QString( "kts" );
    
    // angle
    m_angCoefs[ 0 ] = 1.0f;
    m_angNames[ 0 ] = QString( "rad" );
    
    m_angCoefs[ 1 ] = (float)Utils::rad2deg();
    m_angNames[ 1 ] = QString( "deg" );
    
    // angular velocity
    m_rotCoefs[ 0 ] = 1.0f;
    m_rotNames[ 0 ] = QString( "rad/s" );
    
    m_rotCoefs[ 1 ] = (float)Utils::rad2deg();
    m_rotNames[ 1 ] = QString( "deg/s" );
}

////////////////////////////////////////////////////////////////////////////////

void ComboUnits::on_currentIndexChanged( int index )
{
    switch ( m_unitsType )
    {   
        //case Number: (sic!) handled by default
        case Length:       m_convertFactor = m_lenCoefs[ index ]; break;
        case Velocity:     m_convertFactor = m_velCoefs[ index ]; break;
        case Angle:        m_convertFactor = m_angCoefs[ index ]; break;
        case RotationRate: m_convertFactor = m_rotCoefs[ index ]; break;
        default:           m_convertFactor = m_numCoefs[ index ]; break;
    }
}
